<?php
/**
 * Created by PhpStorm.
 * User: o0无忧亦无佈(285753421@qq.com)
 * Date: 15-2-2
 * Time: 下午5:25
 */

class order extends  MY_User_Controller{

    protected $user_id;
    public function __construct()
    {
        parent::__construct();
        $this->load->model('user/user_order');
        $this->user_id = $this->_login_user['user_id'];
        $this->set_layout('main');

    }

    function index($state = 'all'){

        if($state == 'all'){
            $my_order = $this->user_order->select($this->user_id);
        }else{
            $my_order = $this->user_order->select($this->user_id,$state);
        }

        $this->load->helper('order');
        $this->load->model('public/file_base');
        $i = 0;
        foreach($my_order as $order){
            $message[$i] = json_decode($order['info'],true);
            $my_order[$i]['message'] = $message[$i];
            $i++;
        }
        $this->assign('my_order',$my_order);
        $this->assign('head_title', '我的订单 - 我的账号');
        $this->display();
    }
     /*
     *
	 *确认订单
	 */
    function confirm_order(){

        $order_id = intval(trim($this->input->post('order_id')));
        $order_state = $this->user_order->get_state($order_id);
        $order_config = $this->load->config('order');

        if($order_state!=$order_config['state']['printed']){
            $this->json_response(false,array('field'=>'state'),'订单还未打印啊！亲！');
        }

        $result = $this->user_order->confirm($order_id);

        if(!$result){
            $this->json_response(false,array(),'确认订单失败，请联系管理员');
        }
        $this->json_response(true,array('field'=>'state'),'确认订单成功');
    }

    /*
     *
     *删除订单
     */
    function del_order($order_id){

        $order_id = intval(trim($order_id));
        $order_state = $this->user_order->get_state($order_id);

        $order_config = $this->load->config('order');

        if($order_state!=$order_config['state']['fail_pay'] or $order_state!=$order_config['state']['place'] or $order_state!=$order_config['state']['user_confirm']){
            $this->json_response(false,array('field'=>'state'),'此类型订单不允许删除');
        }

        $result = $this->user_order->del($order_id,$this->user_id);

        if(!$result){
            $this->json_response(false,array(),'删除订单失败，请联系管理员');
        }
        $this->json_response(true,array('field'=>'state'),'订单删除成功,您可以在回收站内看到它们');
    }

}
